
#include "mobile_input.h"


act_map_chars_t act_chars[] =
{
    {VACT_NUM_0,   ACT_TYPE_CHAR,  4,  " _-0"},
    {VACT_NUM_1,   ACT_TYPE_CHAR,  9,  "#.,*?!/@1"},
    {VACT_NUM_2,   ACT_TYPE_CHAR,  4,  "ABC2"},
    {VACT_NUM_3,   ACT_TYPE_CHAR,  4,  "DEF3"},        
    {VACT_NUM_4,   ACT_TYPE_CHAR,  4,  "GHI4"},
    {VACT_NUM_5,   ACT_TYPE_CHAR,  4,  "JKL5"},
    {VACT_NUM_6,   ACT_TYPE_CHAR,  4,  "MNO6"},
    {VACT_NUM_7,   ACT_TYPE_CHAR,  5,  "PQRS7"},
    {VACT_NUM_8,   ACT_TYPE_CHAR,  4,  "TUV8"},
    {VACT_NUM_9,   ACT_TYPE_CHAR,  5,  "WXYZ9"},
    {VACT_EDIT_LEFT,	ACT_TYPE_LEFT,  0,  NULL},
    {VACT_EDIT_RIGHT,	ACT_TYPE_RIGHT, 0,  NULL},

    {VACT_CAPS, 	ACT_TYPE_CAPS,  0,  NULL},
    {VACT_DEL,		ACT_TYPE_DEL,  0,  NULL},
};

#define MOBILE_ACT_CNT	 (sizeof(act_chars)/sizeof(act_chars[0]))


mobile_input_type_t mobile_input_type = 
{
	MOBILE_INPUT_NORMAL,
	MOBILE_CAPS_INIT_LOW,
	17,
	0,
	NULL	
};

static UINT32 mobile_key_press_cnt = 0;
static UINT8  mobile_pre_act;


static UINT32 mobile_key_caps_flag = 0;/* 0-lower case 1-up case */

void mobile_input_init(EDIT_FIELD* edf,mobile_input_type_t *type)
{
	mobile_key_press_cnt = 0;
    mobile_pre_act  = VACT_PASS;
	if(type != NULL)
	{
		mobile_input_type.type 		 = type->type;
		mobile_input_type.caps_flag 	 = type->caps_flag;
		mobile_input_type.maxlen		 = type->maxlen;
		mobile_input_type.fixlen_flag = type->fixlen_flag;	
		mobile_input_type.callback	 = type->callback;
	}
	else
	{
		mobile_input_type.type 		 = MOBILE_INPUT_NORMAL;
		mobile_input_type.caps_flag 	 = MOBILE_CAPS_INIT_LOW;
		mobile_input_type.maxlen		 = 17;
		mobile_input_type.fixlen_flag = 0;	
		mobile_input_type.callback	 = NULL;
	
	}

	if(mobile_input_type.caps_flag == MOBILE_CAPS_INIT_LOW
		|| mobile_input_type.caps_flag == MOBILE_CAPS_ALWAYS_LOW)
		mobile_key_caps_flag = 0;
	else
		mobile_key_caps_flag = 1;

	edf->bCursor = 0;
	edf->bMaxLen = mobile_input_type.maxlen;
}


PRESULT mobile_input_proc(EDIT_FIELD* edf, VACTION vact, UINT32 key, UINT32 para)
{
	PRESULT ret = PROC_LOOP;
	UINT32 i,cur_max_pos;
	ACT_TYPE act_type;
	act_map_chars_t* pact_map;
	char    ch;
	UINT8 	bStyle;

	bStyle = edf->bStyle;
	if(!(bStyle&EDIT_STATUS) &&  (bStyle&SELECT_EDIT_MODE))
		return PROC_PASS;
	
	cur_max_pos = ComUniStrLen(edf->pString);
    if(cur_max_pos >=  edf->bMaxLen)
        cur_max_pos = edf->bMaxLen - 1;
    if(edf->bCursor > cur_max_pos)
        edf->bCursor = cur_max_pos;    	
	for(i = 0;i<MOBILE_ACT_CNT;i++)
	{
		pact_map = &act_chars[i];
		if(pact_map->act == vact)
			break;
	}

	if(i == MOBILE_ACT_CNT)
		return PROC_PASS;

	act_type = pact_map->acttype;

	switch(act_type)
	{
	case ACT_TYPE_CHAR:
		if(mobile_pre_act != VACT_PASS  && mobile_pre_act != vact)
		{
			mobile_key_press_cnt = 0;
			if (edf->bCursor < cur_max_pos ) 
				edf->bCursor ++;
		}
		mobile_pre_act = vact;

		mobile_key_press_cnt %= pact_map->map_char_count;
		ch = pact_map->map_chars[mobile_key_press_cnt];

		if(mobile_input_type.type == MOBILE_INPUT_NORMAL)
		{
			
			mobile_key_press_cnt ++;
			mobile_key_press_cnt %= pact_map->map_char_count;
		}
		else // MOBILE_INPUT_HEX
		{
			if( (ch>='0' && ch<='9') || (ch>='a' && ch<='f') || (ch>='A' && ch<='F') )
			{
				mobile_key_press_cnt ++;
				mobile_key_press_cnt %= pact_map->map_char_count;
			}
			else
			{
				for(i=0;i<pact_map->map_char_count;i++)
				{
					ch = pact_map->map_chars[ (i + mobile_key_press_cnt) % pact_map->map_char_count];
					if( (ch>='0' && ch<='9') || (ch>='a' && ch<='f') || (ch>='A' && ch<='F') )
					{
						mobile_key_press_cnt ++;
						mobile_key_press_cnt %= pact_map->map_char_count;
						break;
					}
				}

				if(i == pact_map->map_char_count)
					return PROC_PASS;
			}
			
		}
		
		if(mobile_key_caps_flag == 0)   /* Lower Case*/
        {
            if(ch>='A' && ch<='Z')
                ch = ch - 'A' + 'a';
        }
        else if(mobile_key_caps_flag == 1)  /* Up Case*/
        {
            if(ch>='a' && ch<='z')
                ch = ch - 'a' + 'A';
        }
		SetUniStrCharAt(edf->pString,ch,edf->bCursor);
		if(edf->bCursor == cur_max_pos)
			edf->pString[edf->bCursor + 1] = 0;
		if(mobile_input_type.callback != NULL)
			mobile_input_type.callback((UINT8*)(edf->pString));		
		break;
	case ACT_TYPE_LEFT:
		if(edf->bCursor > 0)
			edf->bCursor --;
		else
			edf->bCursor = cur_max_pos;

		mobile_key_press_cnt = 0;
		mobile_pre_act = VACT_PASS;
		break;
	case ACT_TYPE_RIGHT:    
		if(edf->bCursor < cur_max_pos)
			edf->bCursor ++;
		else
			edf->bCursor = 0;
		mobile_key_press_cnt = 0;
		mobile_pre_act = VACT_PASS;
		break;
	case ACT_TYPE_CAPS:
		if(mobile_input_type.caps_flag != MOBILE_CAPS_ALWAYS_LOW
			&& mobile_input_type.caps_flag != MOBILE_CAPS_ALWAYS_UP)			
			mobile_key_caps_flag = mobile_key_caps_flag == 0? 1 : 0;
		break;
	case ACT_TYPE_DEL:
		if(cur_max_pos > 0)
		{
			if( !mobile_input_type.fixlen_flag)
			{
				for(i=edf->bCursor;i<cur_max_pos;i++)
					edf->pString[i] = edf->pString[i+1];
				edf->pString[cur_max_pos] = 0;
				mobile_key_press_cnt = 0;
				mobile_pre_act = VACT_PASS;
				if(mobile_input_type.callback != NULL)
					mobile_input_type.callback((UINT8*)(edf->pString));
			}
		}
		break;
	default:
		break;
	}

	OSD_TrackObject((POBJECT_HEAD)edf, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
	
	return ret;
}

UINT8  mobile_input_get_caps(void)
{
	return mobile_key_caps_flag ;
}


